// pages/product/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    topbar: [],
    info: [],
    lists: [],
    active: 0,
    page: 1,
    totalpage: "",
    limit: 10,

  },


  onTabChange(event) {
    const index = event.detail.index;
    console.log(event.detail);
    let id = this.data.topbar[index].id;
    
    this.setData({
      id: id,
      active: index, // 添加这一行，同步更新 active
      page: 1, // 重置页码，因为切换分类要重新加载数据
      lists: [] // 清空列表数据，准备加载新分类的数据
    });
    
    console.log(this.data.id);
    this.getTop()
    this.getinfos()
  },

  getTop() {
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/goods/category',
      data: {
        id: this.data.id
      },
      method: "GET",
      success: (res) => {
        if (res.statusCode == '200') {
          const topbar = res.data.data.brotherCategory;
          const currentCategory = res.data.data.currentCategory;
          
          // 统一转换为字符串进行比较
          const currentId = String(this.data.id);
          
          const currentIndex = topbar.findIndex(item => String(item.id) === currentId);
          console.log('统一类型后的匹配索引:', currentIndex);
          
          this.setData({
            topbar: topbar,
            info: currentCategory,
            active: currentIndex !== -1 ? currentIndex : 0
          });
        }
      },
      fail: function (error) {
        console.error('获取分类失败:', error);
      }
    })
  },

  getinfos() {
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/goods/list',
      data: {
        categoryId: this.data.id,
        page: this.data.page,
        limit: this.data.limit
      },
      method: "GET",
      success: (res) => {
        console.log(res)
        if (res.statusCode == '200') {

          let newList = res.data.data.list;
          let totalPage = res.data.data.pages;


          let updatedList = [];
          if (this.data.page === 1) {
            updatedList = newList;
          } else {
            updatedList = [...this.data.lists, ...newList];
          }
          this.setData({
            lists: updatedList,
            totalpage: totalPage
          });

          console.log(this.data.totalpage)
          console.log(this.data.lists)
        }
      },
      fail: function (error) {
        console.error('请求失败:', error);
      },
      complete: function () {
        // 可以在这里处理完成后的逻辑
      }
    })
  },


  ongoodsTap(event) {
    // console.log(event);
    let id = event.currentTarget.dataset.id
    console.log(id);
    wx.navigateTo({
      url: `/pages/goods/goods?id=${id}`
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id;
    console.log(id);
  
    this.setData({
      id: id,
      page: 1,
      active: 0 // 确保初始 active 为 0
    });
    
    this.getTop()
    this.getinfos()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getinfos()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page < this.data.totalpage) {
      this.setData({
        page: this.data.page + 1
      });
      this.getinfos();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})